﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TransformCost.Question;

namespace TransformCost.Answer
{
    class Effie:ITransformCost
    {

        public int Exchange(List<int> outsource, int x, out List<int> tempoutsource)
        {
           int n=outsource.LastIndexOf(x);
            int m=0;
            int steps = 0;
            for (int j = n ; j < outsource.Count-1; j++)
            {
                m=outsource[j] ;
                outsource[j] = outsource[j + 1];
                outsource[j + 1] = m;
                steps += 1;
            }
            tempoutsource=outsource;
            return steps;

        }

        public int GetMinExchanges(int[] source, int[] target)
        {
            int minnum = 0;
            List<int> outsource=new List<int>();
           List<int> tempoutsource=new List<int>();
           List<int> outtarget=new List<int>();
            for (int i=0;i < target.Length; i++)
            {
                outsource.Add(source[i]);
                outtarget.Add(target[i]);
            }
            for (int i = outtarget.Count-1; i >=0; i--)
           {

               minnum+=this.Exchange(outsource, outtarget[i], out tempoutsource);
                outtarget = outtarget.GetRange(0, outtarget.Count-1);
                outsource = tempoutsource.GetRange(0, tempoutsource.Count - 1);
           }
            return minnum;
        }
    }
}
